package com.biubiux2.session;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author noexcs
 * @since 8/10/2022 2:47 PM
 */
public class SessionMap {

    /**
     * key: session id
     * value: session
     */
    public ConcurrentHashMap<String, ServerSession> sessionMap = new ConcurrentHashMap<>();

    public ConcurrentHashMap<Integer, ServerSession> uidSessionMap = new ConcurrentHashMap<>();

    public static ScheduledExecutorService removeSessionService = new ScheduledThreadPoolExecutor(10);

    private SessionMap() {
    }

    private static class SessionMapLazyHolder {
        public static final SessionMap SESSION_MAP = new SessionMap();
    }

    public static SessionMap getInstance() {
        return SessionMapLazyHolder.SESSION_MAP;
    }

    public void addSession(ServerSession session) {
        sessionMap.put(session.getSessionId(), session);
        uidSessionMap.put(session.userTransmission.getUser().getUserId(), session);
    }

}
